import requests,re

# login_url = 'http://127.0.0.1:8000/admin/login/?next=/admin/login/'
#
# response_login = requests.get(login_url,data={'username':'admin','password':'123456'})
#
# csrfmiddlewaretoken = re.search(r'<input type="hidden" name="csrfmiddlewaretoken" value="(.*?)">',response_login.text).group(1)
#
#
# response_login = requests.post(login_url,data={
#     'csrfmiddlewaretoken':csrfmiddlewaretoken,
#     'username':'admin',
#     'password':'123456'
# },cookies=response_login.cookies)
#
#
# print(response_login.text)

login_url = 'http://127.0.0.1:8000/admin/login/?next=/admin/login/'
session = requests.Session()
print(session.cookies)
response = session.get(login_url)

csrfmiddlewaretoken = re.search(r'<input type="hidden" name="csrfmiddlewaretoken" value="(.*?)">',response.text).group(1)


response = session.post(login_url,data={
    'csrfmiddlewaretoken':csrfmiddlewaretoken,
    'username':'admin',
    'password':'123456'
})


print(response.text)